      SUBROUTINE PK_SECT1(KFILDO,IPACK,ND5,IS1,NS1,NEW,L3264B,
     1                    LOCN,IPOS,IER,ISEVERE,*)
C
C        MARCH   2000   LAWRENCE   GSC/TDL   ORIGINAL CODING
C        JANUARY 2001   GLAHN      COMMENTS; CHANGED IS1(5) TEST; ADDED
C                                  TEST FOR SIZE OF IS1( )
C
C        PURPOSE
C            PACKS SECTION 1, THE IDENTIFICATION SECTION, OF A GRIB2
C            MESSAGE.
C
C        DATA SET USE
C           KFILDO - UNIT NUMBER FOR OUTPUT (PRINT) FILE. (OUTPUT)
C
C        VARIABLES
C              KFILDO = UNIT NUMBER FOR OUTPUT (PRINT) FILE. (INPUT)
C            IPACK(J) = THE ARRAY THAT HOLDS THE ACTUAL PACKED MESSAGE
C                       (J=1,ND5). (INPUT/OUTPUT)
C                 ND5 = THE SIZE OF THE ARRAY IPACK( ). (INPUT)
C              IS1(J) = CONTAINS THE IDENTIFICATION DATA THAT 
C                       WILL BE PACKED INTO IPACK( ) (J=1,NS1).
C                       (INPUT)
C                 NS1 = SIZE OF IS1( ). (INPUT) 
C                 NEW = WHEN NEW = 1, THIS IS THE FIRST GRID TO BE 
C                       PACKED IN THE GRIB2 MESSAGE. THERE MUST BE
C                       A SECTION 1 IN THIS CASE. WHEN NEW = 0,
C                       THIS IS NOT THE FIRST GRID TO BE PACKED INTO
C                       THIS MESSAGE. THERE SHOULD NOT BE A SECTION 
C                       1 IN THIS CASE. 
C              L3264B = THE INTEGER WORD LENGTH IN BITS OF THE MACHINE
C                       BEING USED. VALUES OF 32 AND 64 ARE
C                       ACCOMMODATED. (INPUT)
C                LOCN = THE WORD POSITION TO PLACE THE NEXT VALUE.
C                       (INPUT/OUTPUT)
C                IPOS = THE BIT POSITION IN LOCN TO START PLACING
C                       THE NEXT VALUE. (INPUT/OUTPUT)
C                 IER = RETURN STATUS CODES. (OUTPUT)
C                         0 = GOOD RETURN.
C                       1-4 = ERROR CODES GENERATED BY PKBG. SEE THE 
C                             DOCUMENTATION IN THE PKBG ROUTINE.
C                       5,6 = ERROR CODES GENERATED BY THE LENGTH
C                             FUNCTION. SEE THE DOCUMENTATION FOR
C                             THE LENGTH FUNCTION.
C                       101 = IS1(5) NE 1 SIGNIFYING SECTION 1.
C                       102 = IS1( ) HAS NOT BEEN DIMENSIONED LARGE
C                             ENOUGH.
C             ISEVERE = THE SEVERITY LEVEL OF THE ERROR.  THE ONLY
C                       VALUE RETUNED IS:
C                       2 = A FATAL ERROR  (OUTPUT)
C                   * = ALTERNATE ERROR RETURN. (OUTPUT)
C
C             LOCAL VARIABLES
C               IPOS1 = SAVES THE BIT POSITION IN LOCN1
C                       TO STORE THE LENGTH OF SECTION 1
C                       AFTER THE ROUTINE IS DONE PACKING
C                       DATA INTO THE SECTION.
C               IZERO = CONTAINS THE VALUE '0'.  THIS IS USED IN THE
C                       CODE SIMPLY TO EMPHASIZE THAT A CERTAIN 
C                       GROUP OF OCTETS IN THE MESSAGE ARE BEING 
C                       ZEROED OUT.
C                   K = A LOOPING INDEX VARIABLE.
C                LOC1 = SAVES THE WORD POSITION IN IPACK
C                       TO STORE THE LENGTH OF SECTION 1
C                       AFTER THE ROUTINE IS DONE PACKING
C                       DATA INTO THE SECTION.
C                   N = L3264B = THE INTEGER WORD LENGTH IN BITS OF
C                       THE MACHINE BEING USED. VALUES OF 32 AN
C                       64 ARE ACCOMMODATED
C             MINSIZE = THE MINIMUM SIZE FOR IS1( ).  IS2(21) IS
C                       FILLED IN PK_SECT1.  THIS ONLY APPLIES WHEN
C                       THIS IS A "NEW" MESSAGE.      
C
C        NON SYSTEM SUBROUTINES CALLED
C           LENGTH,PKBG
C
      PARAMETER(MINSIZE=21)
C
      DIMENSION IPACK(ND5),IS1(NS1)
C
      DATA IZERO/0/
C
      N=L3264B
      IER=0
C
C        ALL ERRORS GENERATED BY THIS ROUTINE ARE FATAL.
      ISEVERE=2
C
C        IN THE CASE OF PACKING A GRIB2 MESSAGE WITH MULTIPLE
C        GRIDS DETERMINE IF A SECTION 1 IS REQUIRED.
C
      IF(NEW.EQ.1)THEN
C
C        CHECK MINIMUM SIZE OF IS1( ).
C
         IF(NS1.LT.MINSIZE)THEN
            IER=102
            GO TO 900
         ENDIF
C
         IF(IS1(5).NE.1)THEN
            IER=101
            GO TO 900
         ENDIF
C
         LOC1 = LOCN
         IPOS1 = IPOS
C
C           THE FIRST FOUR BYTES HOLD THE LENGTH OF THE SECTION.
C           ZERO THEM OUT FOR NOW, AND FILL THEM IN LATER.
         CALL PKBG(KFILDO,IPACK,ND5,LOCN,IPOS,IZERO,32,N,IER,*900)
C
C           PACK THE NUMBER OF THE SECTION.
         CALL PKBG(KFILDO,IPACK,ND5,LOCN,IPOS,1,8,N,IER,*900)
C
C           PACK ID OF ORIGINATING/GENERATING CENTRE
         CALL PKBG(KFILDO,IPACK,ND5,LOCN,IPOS,IS1(6),16,N,IER,*900)
C
C           PACK ID OF ORIGINATING/GENERATING SUB-CENTRE
         CALL PKBG(KFILDO,IPACK,ND5,LOCN,IPOS,IS1(8),16,N,IER,*900)
C
C           PACK GRIB MASTER AND LOCAL TABLES VERSION NUMBERS.
         CALL PKBG(KFILDO,IPACK,ND5,LOCN,IPOS,IS1(10),8,N,IER,*900)
         CALL PKBG(KFILDO,IPACK,ND5,LOCN,IPOS,IS1(11),8,N,IER,*900)
C
C           PACK SIGNIFICANCE OF REFERENCE TIME
         CALL PKBG(KFILDO,IPACK,ND5,LOCN,IPOS,IS1(12),8,N,IER,*900)
C
C           PACK THE YEAR, DAY, MONTH, HOUR, MINUTE, AND SECOND
C           OF THE DATA.
         CALL PKBG(KFILDO,IPACK,ND5,LOCN,IPOS,IS1(13),16,N,IER,*900)
C
         DO K=15,19
            CALL PKBG(KFILDO,IPACK,ND5,LOCN,IPOS,IS1(K),8,N,IER,*900)
         ENDDO
C
C           PACK THE PRODUCTION STATUS AND TYPE OF PROCESSED DATA
         CALL PKBG(KFILDO,IPACK,ND5,LOCN,IPOS,IS1(20),8,N,IER,*900)
         CALL PKBG(KFILDO,IPACK,ND5,LOCN,IPOS,IS1(21),8,N,IER,*900)
C
C           COMPUTE THE LENGTH OF THE SECTION AND PACK IT.  LOC1 AND
C           IPOS1 REPRESENT THE POSITION OF THE FIRST BYTE IN SECTION
C           1.  8 IS THE NUMBER OF BITS IN A BYTE, AND EACH SECTION
C           ENDS AT THE END OF A BYTE.
         IS1(1)=LENGTH(KFILDO,IPACK,ND5,L3264B,LOC1,IPOS1,LOCN,
     1                 IPOS,IER)
      ENDIF
C
C       ERROR RETURN SECTION
 900  IF(IER.NE.0)RETURN 1
C
      RETURN
C
      END
